/**
 * Project  : Looker
 * Package  : net.freestu.looker.util
 * File     : FileUtils.java
 * Author   : solosky < solosky772@qq.com >
 * Date     : 2009-9-5
 * Modified : 2009-9-5
 * License  : Apache License 2.0 
 */
package net.freestu.looker.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.poi.POITextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.xmlbeans.XmlException;

/**
 *  
 */
public class FileUtils
{
	public static enum FileType {IMAGE, VIDEO, AUDIO, DOC, ZIP, OHTER};
	
	/**
	 * 复制文件到指定的地方
	 * @param src
	 * @param dist
	 * @throws IOException 
	 */
	public static void copyTo(File src, File dist) throws IOException
	{
		if(!src.exists()|| src.isDirectory())
			throw new FileNotFoundException("需复制的文件不存在或者是目录");
		if(dist.isDirectory())	//拷贝到该目录下
			dist = new File(dist.getAbsolutePath()+"/"+src.getName());
		InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dist);
        byte[] bytes = new byte[1024];
        int readed = 0;
        while( (readed=in.read(bytes)) !=-1)
        		out.write(bytes, 0, readed);
        out.close();
        in.close();
	}
	
	/**
	 * 给定一个文件名，返回文件的扩展名
	 */
	public static String extension(String fileName)
	{
		int pos = fileName.lastIndexOf(".");
		return pos==-1 || fileName.length()-1==pos ? null : fileName.substring(pos+1);
	}
	
	
	public static FileType fileType(String extension)
	{
		String imageType = "jpg,png,gif,bmp";
		String videoType = "rmvb,wmv";
		String audioType = "mp3,wma";
		String zipType   = "zip,rar,7z,bz,tar";
 		String docType   =  "doc,xls,ppt,docx,xlsx,pptx,pdf";
		
		if(imageType.indexOf(extension)!=-1)
			return FileType.IMAGE;
		else if(videoType.indexOf(extension)!=-1)
			return FileType.VIDEO;
		else if (audioType.indexOf(extension)!=-1)
			return FileType.AUDIO;
		else if(docType.indexOf(extension)!=-1)
			return FileType.DOC;
		else if(zipType.indexOf(extension)!=-1)
			return FileType.ZIP;
		else
			return FileType.OHTER;
	}
	
	/**
	 * 
	 * @param args
	 * @throws IOException
	 */
	
	public static void main(String []args) throws IOException
	{
		File dir = new File("E:\\App\\Java\\looker3\\media\\www1.cdut.edu.cn\\2009-7\\d\\");
		File[] docs = dir.listFiles();
		for(int i=0; i<docs.length; i++) {
			POITextExtractor ext = null;
            try {
	            ext = ExtractorFactory.createExtractor(docs[i]);
	            String text = ext.getText();
	        	System.out.println(docs[i].getName()+" - "+parseTitle(text));
            } catch (InvalidFormatException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
            } catch (OpenXML4JException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
            } catch (XmlException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
            } catch (Exception e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
         }			
		}
		
	}
	
	public static String parseTitle(String text)
	{
		int p = text.indexOf('\n');
		String ret = null;
		if(p>30) {
			
			ret = text.substring(0,p);
		}else {
			ret =  text.substring(0,30);
		}
		return ret;
	}
	
}
